package com.hsteach.service.rota;

import java.util.List;

import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.DateUtils;
import com.hsteach.enums.DutyRoleEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hsteach.dao.mapper.rota.RotaPersonMapper;
import com.hsteach.dao.domain.rota.RotaPerson;

/**
 * 值班人员Service业务层处理
 * 
 * @author fanxquer
 * @date 2023-10-18
 */
@Service
public class RotaPersonService
{
    @Autowired
    private RotaPersonMapper rotaPersonMapper;

    /**
     * 查询值班人员
     * 
     * @param id 值班人员主键
     * @return 值班人员
     */
    public RotaPerson selectRotaPersonById(Long id)
    {
        return rotaPersonMapper.selectRotaPersonById(id);
    }

    /**
     * 查询值班人员列表
     * 
     * @param rotaPerson 值班人员
     * @return 值班人员
     */
    public List<RotaPerson> selectRotaPersonList(RotaPerson rotaPerson)
    {
        return rotaPersonMapper.selectRotaPersonList(rotaPerson);
    }

    /**
     * 新增值班人员
     * 
     * @param rotaPerson 值班人员
     * @return 结果
     */
    public int insertRotaPerson(RotaPerson rotaPerson)
    {
        if (DutyRoleEnum.LEADER.getCode().equals(rotaPerson.getDutyRole())) {
            RotaPerson query = new RotaPerson();
            query.setRotaDetailId(rotaPerson.getRotaDetailId());
            query.setDutyRole(DutyRoleEnum.LEADER.getCode());
            List<RotaPerson> rotaPeople = rotaPersonMapper.selectRotaPersonList(query);
            if (rotaPeople.size() > 0) {
                throw new ServiceException("只能有一个值班领导");
            }
        }
        rotaPerson.setCreateTime(DateUtils.getNowDate());
        return rotaPersonMapper.insertRotaPerson(rotaPerson);
    }

    /**
     * 修改值班人员
     * 
     * @param rotaPerson 值班人员
     * @return 结果
     */
    public int updateRotaPerson(RotaPerson rotaPerson)
    {
        rotaPerson.setUpdateTime(DateUtils.getNowDate());
        return rotaPersonMapper.updateRotaPerson(rotaPerson);
    }

    /**
     * 批量删除值班人员
     * 
     * @param ids 需要删除的值班人员主键
     * @return 结果
     */
    public int deleteRotaPersonByIds(Long[] ids)
    {
        return rotaPersonMapper.deleteRotaPersonByIds(ids);
    }

    /**
     * 删除值班人员信息
     * 
     * @param id 值班人员主键
     * @return 结果
     */
    public int deleteRotaPersonById(Long id)
    {
        return rotaPersonMapper.deleteRotaPersonById(id);
    }
}
